package com.ttg.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ttg.pojo.PaymentRespDTO;
import com.ttg.pojo.RefundGoodsAsyncRespDTO;
import com.ttg.pojo.SyncInfoDTO;
import com.ttg.security.EncryptionUtils;
import com.ttg.utils.HttpKit;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

@Controller
@RequestMapping("/callback")
public class GwCallbackController  {
    private static final Logger LOGGER = LoggerFactory.getLogger(GwCallbackController.class);

    @RequestMapping("/test_pay_callback")
    @ResponseBody
    public String testPayCallback(@RequestBody String callStr,HttpServletRequest request){
        LOGGER.debug("接收到的支付回调信息为：{}",callStr);
        SyncInfoDTO syncInfoDTO = JSON.toJavaObject((JSON) JSONObject.parse(callStr),SyncInfoDTO.class);
        LOGGER.debug("data:{}",syncInfoDTO.getData().get(0));
        PaymentRespDTO paymentRespDTO = JSON.toJavaObject((JSON) syncInfoDTO.getData().get(0),PaymentRespDTO.class) ;
        String sign = HttpKit.httpGwParam2SignStr(paymentRespDTO.getAllParameters());
        try {
            boolean flag = EncryptionUtils.verifySign(sign.getBytes(),paymentRespDTO.getSignatureInfo().getBytes(),Thread.currentThread().getContextClassLoader().getResource("key/mock.cer").getPath());
            LOGGER.debug("验签结果：{}",flag);
        } catch (Exception e) {
            e.printStackTrace();
        }
        LOGGER.debug("接收到的参数：{},参与签名串：{}", paymentRespDTO,
                HttpKit.httpGwParam2SignStr(paymentRespDTO.getAllParameters()));
        LOGGER.info("商户号：{},订单号：{},接收时间：{}",paymentRespDTO.getMerchantId(),paymentRespDTO.getMerOrderId(),new Date());
        return "SUCCESS";
    }

    @RequestMapping("/test_refund_callback")
    @ResponseBody
    public String testRefundCallback(@RequestBody String callStr,HttpServletRequest request){
        LOGGER.debug("接收到的退款回调信息为：{}",callStr);
        SyncInfoDTO syncInfoDTO = JSON.toJavaObject((JSON) JSONObject.parse(callStr),SyncInfoDTO.class);
        LOGGER.debug("data:{}",syncInfoDTO.getData().get(0));
        RefundGoodsAsyncRespDTO refundGoodsAsyncRespDTO =JSON.toJavaObject((JSON) syncInfoDTO.getData().get(0),RefundGoodsAsyncRespDTO.class);
        String sign = HttpKit.httpGwParam2SignStr(refundGoodsAsyncRespDTO.getAllParameters());
        try {
            boolean flag = EncryptionUtils.verifySign(sign.getBytes(),refundGoodsAsyncRespDTO.getSignatureInfo().getBytes(),Thread.currentThread().getContextClassLoader().getResource("key/mock.cer").getPath());
            LOGGER.debug("验签结果：{}",flag);
        } catch (Exception e) {
            e.printStackTrace();
        }
        LOGGER.debug("接收到的参数：{},参与签名串：{}", refundGoodsAsyncRespDTO,
                HttpKit.httpGwParam2SignStr(refundGoodsAsyncRespDTO.getAllParameters()));
        return "SUSCCESS";
    }

    @RequestMapping("/test_callback_pg")
    public void testPgCallback(HttpServletRequest request){
        LOGGER.debug("接收到的页面回调信息：{}",request.getParameterMap());
    }
}
